<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆页面</title>
    <style type="text/css">
        #main{
            width: 400px;
            margin: 100px auto;
        }
        input{
            border-radius: 5px;
            width: 200px;
            height: 20px;
        }
        button{
            margin-left: 30px;
            width: 80px;
            border-radius: 15px;
        }
    </style>
</head>
<body>

<div id="main">
    <font >实打实</font>
    <form id="loginForm" action="login" method="post" >
        <font color="red" th:text="${msg}"></font><br><br>
        帐号：<input type="text" id="username" name="username"><br><br>
        密码：<input type="password" id="password" name="password"><br><br>
        <input type="hidden" id="token" name="token">
        <button   id="login_btn"  type="button">登录</button>
    </form>
</div>
</body>
<script type="text/javascript" th:src="@{/static/js/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/static/js/jquery.form.js}"></script>
<script>
   $(function () {

      $("#login_btn").click(function () {
          var username =$("#username").val();
          var password =$("#password").val();
          $.ajax({
              url: "/getToken",
              async:false,
              data:JSON.stringify({'username':username,'password':password}),
              type: "POST",
              contentType:'application/json;charset=UTF-8',
              dataType:'Json',
              success:function (data) {
                  //成功获取Token
                  /*alert(JSON.stringify(data.token))*/
                  $("#token").val(JSON.stringify(data.token))
                  $("#loginForm").submit();
                 /* $("#loginForm").submit();*/



                 /* 提交表单*/
         /*    $.ajax({
                      url: "/login",
                      type: "POST",
                      data:JSON.stringify({'username':username,'password':password}),
                      headers:{'token':JSON.stringify(data.token)},
                      dataType:'json',
                      contentType:'application/json;charset=UTF-8',
                      success:function (data) {
                         alert(JSON.stringify(data))
                          alert("登录成功!")
                      },
                      error:function(){
                          alert("回調失敗！！");
                      }
                  });*/
              },
              error:function(){
                  alert("登录失败！！");
              }
          });

      });
    })
</script>
</html>
